/**
 * controller_radio.js
 * 电台相关控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-04-28
 */

//晨间电台
app.controller('RadioMorningList', function($scope, $http, $location, common){
	$scope.list 	= [];
	$scope.loading 	= false;
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.aItem 	= {'is_try' : 0};
	$scope.nItem 	= {};
	$scope.isTry 	= 'name';

	var aContent, nContent;
	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-banner-edit, #upload-audio-edit').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.aItem.banner 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio':
							$scope.aItem.audio 	= '/server'	+ info.data.info.path;
						break;
						case 'upload-banner-edit':
							$scope.nItem.banner 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio-edit':
							$scope.nItem.audio 	= '/server' + info.data.info.path;
						break;
					}
				})
			}
		});

		//标签页钩子设置
		$('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            $scope.list 	= [];
            switch(type){
                case '#list-all': $scope.isTry 	= ''; break;
                case '#list-normal'	: $scope.isTry 	= 0; break;
                case '#list-try'	: $scope.isTry 	= 1; break;
                default: break;
            }
            $scope.get_list(1);
        });

        aContent 	= common.createEditor('a-content');
	}

	//加载数据
	$scope.get_list 	= function(page){
		if(!page)	page = 1;
		var url 	= config.prefix_url + '/RadioMorning/getList?p='+page;
		var param 	= {
			'condition[is_try]'	: $scope.isTry	
		}

		$http.get(url+'&'+objectToHttp(param)).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml 	= info.data.page_html;
			$scope.loading 	= false;

			common.startPage($scope.get_list);
		})

		$scope.loading 	= true;
	}

	//添加电台
	$scope.add_item 	= function(){
		$location.path('/RadioMorning/addItem');
		return true;

		// var url 	= config.prefix_url + '/RadioMorning/addItem';
		// var param 	= $scope.aItem;

		// $http.post(url, param).success(function(info){
		// 	$scope.loading 	= false;
		// 	if(info.code != 1){
		// 		common.dealError(info);
		// 		return false;
		// 	}
		// 	$scope.get_list($scope.nPage);
		// 	$('.modal.add-radio').modal('hide');
		// })
		// $scope.loading 	= true;
	}

	//编辑电台
	$scope.get_item 	= function(radio){
		S('radio_morning_info', radio);
		$location.path('/RadioMorning/getItem');
		// $scope.nItem 	= radio;
		// $('.modal.edit-radio').modal('show');
	}

	//保存编辑
	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url + '/RadioMorning/updateItem';
		var param 	= {
			'radio_id'	: id,
			'radio_info'	: info
		}

		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$('.modal.edit-radio').modal('hide');
			$scope.get_list($scope.nPage);
		})

		$scope.loading 	= true;
	}

	//删除电台
	$scope.delete 	= function(id){
		var url 	= config.prefix_url + '/RadioMorning/updateItem';
		var param 	= {
			'radio_id'	: id,
			'radio_info': {status : -1}
		}
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.get_list($scope.nPage);
		})

		$scope.loading 	= true;
	}

	$scope.get_list();
	$scope.init_js()
})

//晨间电台添加
app.controller('RadioMorningAdd', function($scope, $http, $location, common){
	//变量声明
	$scope.nItem 	= {};
	$scope.loading 	= false;

	//富文本框变量声明
	// var eContent;

	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-i-audio').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.nItem.banner 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio':
							$scope.nItem.audio 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-i-audio':
							$scope.nItem.i_audio 	= config.host_name + '/server' + file.path;
						break;
					}
				})
			}
		});

		$('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd hh:ii',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 0
        });

		// eContent 	= common.createEditor('content');
	}

	//添加条目
	$scope.add_item 	= function(){
		var url 	= config.prefix_url + '/RadioMorning/addItem';
		var item 	= $scope.nItem;
		// item.content 	= eContent.$txt.html();
		
		$http.post(url, item).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('添加成功');
			$scope.back_url();
		})

		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/RadioMorning/getList');
	}

	$scope.init_js();
})

//晨间电台详情
app.controller('RadioMorningGet', function($scope, $http, $location, common){
	$scope.nItem 	= S('radio_morning_info')

	if(!$scope.nItem){
		common.doAlert('活动无效!');
		$scope.back_url();
	}

	//富文本框变量声明
	var eContent;

	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-i-audio').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.nItem.banner 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio':
							$scope.nItem.audio 	= config.host_name + '/server' + file.path;
						break;
					}
				})
			}
		});
		// eContent 	= common.createEditor('content');
		// eContent.$txt.html($scope.nItem.content);
	}

	//更新数据
	$scope.update_item 	= function(){
		var url 	= config.prefix_url + '/RadioMorning/updateItem';
		var item 	= $scope.nItem;
			// item.content 	= eContent.$txt.html();
		var param 	= {
			'radio_id'	: item.id,
			'radio_info'	: item
		};
		
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('更新成功');
			$scope.back_url();
		})

		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/RadioMorning/getList');
	}

	$scope.init_js();
})

//晚间电台
app.controller('RadioEveningList', function($scope, $http, $location, common){
	$scope.list 	= [];
	$scope.loading 	= false;
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.aItem 	= {};
	$scope.nItem 	= {};


	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-banner-edit, #upload-audio-edit, #upload-audio-ii, #upload-audio-edit-ii').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.aItem.banner 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio':
							$scope.aItem.audio 	= '/server'	+ info.data.info.path;
						break;
						case 'upload-audio-ii':
							$scope.aItem.audio_ii 	= '/server'	+ info.data.info.path;
						break;
						case 'upload-banner-edit':
							$scope.nItem.banner 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio-edit':
							$scope.nItem.audio 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio-edit-ii':
							$scope.nItem.audio_ii 	= '/server' + info.data.info.path;
						break;
						
					}
				})
			}
		});

		$('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd hh:ii',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 0
        });
	}

	//加载数据
	$scope.load_data 	= function(page){
		if(!page)	page = 1;
		var url 	= config.prefix_url + '/RadioEvening/getList?p='+page;
		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml 	= info.data.page_html;
			$scope.loading 	= false;

			common.startPage($scope.load_data);
		})

		$scope.loading 	= true;
	}

	//添加电台
	$scope.add_item 	= function(item){
		$location.path('/RadioEvening/addItem');
		// var url 	= config.prefix_url + '/RadioEvening/addItem';
		// var param 	= item;
		// $http.post(url, param).success(function(info){
		// 	$scope.loading 	= false;
		// 	$('.modal.add-radio').modal('hide');
		// 	if(info.code != 1){
		// 		common.dealError(info);
		// 		return false;
		// 	}
		// 	$scope.load_data($scope.nPage);
		// })
		// $scope.loading 	= true;
	}

	//查看电台
	$scope.get_item 	= function(item){
		S('radio_evening_info', item);
		$location.path('/RadioEvening/getItem');
		// $scope.nItem 	= item;
		// $('.modal.edit-radio').modal('show');
	}

	//保存编辑
	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url + '/RadioEvening/updateItem';
		var param 	= {
			'radio_id'	: id,
			'radio_info'	: info
		}

		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$('.modal.edit-radio').modal('hide');
			$scope.load_data($scope.nPage);
		})

		$scope.loading 	= true;
	}

	$scope.load_data();
	$scope.init_js()
})

//晚间电台添加
app.controller('RadioEveningAdd', function($scope, $http, $location, common){
	//变量声明
	$scope.nItem 	= {};
	$scope.loading 	= false;

	//富文本框变量声明
	// var eContent;

	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-audio-ii').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.nItem.banner 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio':
							$scope.nItem.audio 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio-ii':
							$scope.nItem.audio_ii 	= config.host_name + '/server' + file.path;
						break;
					}
				})
			}
		});

		$('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd hh:ii',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 0
        });

		// eContent 	= common.createEditor('content');
	}

	//添加条目
	$scope.add_item 	= function(){
		var url 	= config.prefix_url + '/RadioEvening/addItem';
		var item 	= $scope.nItem;
		// item.content 	= eContent.$txt.html();
		
		$http.post(url, item).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('添加成功');
			$scope.back_url();
		})

		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/RadioEvening/getList');
	}

	$scope.init_js();
})

//晚间电台详情
app.controller('RadioEveningGet', function($scope, $http, $location, common){
	$scope.nItem 	= S('radio_evening_info')
	if(!$scope.nItem){
		common.doAlert('活动无效!');
		$scope.back_url();
	}

	//富文本框变量声明
	// var eContent;

	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-audio-ii').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.nItem.banner 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio':
							$scope.nItem.audio 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio-ii':
							$scope.nItem.audio_ii 	= config.host_name + '/server' + file.path;
						break;
					}
				})
			}
		});
		// eContent 	= common.createEditor('content');
		// eContent.$txt.html($scope.nItem.content);
	}

	//更新数据
	$scope.update_item 	= function(){
		var url 	= config.prefix_url + '/RadioEvening/updateItem';
		var item 	= $scope.nItem;
			// item.content 	= eContent.$txt.html();
		var param 	= {
			'radio_id'	: item.id,
			'radio_info'	: item
		};
		
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('更新成功');
			$scope.back_url();
		})

		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/RadioEvening/getList');
	}

	$scope.init_js();
})

//电台故事
app.controller('RadioStoryList', function($scope, $http, common){
	$scope.list 	= [];
	$scope.loading 	= false;
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.aItem 	= {};
	$scope.nItem 	= {};


	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-banner-edit, #upload-audio-edit').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.aItem.banner 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio':
							$scope.aItem.audio 	= '/server'	+ info.data.info.path;
						break;
						case 'upload-banner-edit':
							$scope.nItem.banner 	= '/server' + info.data.info.path;
						break;
						case 'upload-audio-edit':
							$scope.nItem.audio 	= '/server' + info.data.info.path;
						break;
					}
				})
			}
		});

		$('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd hh:ii',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 0
        });
	}

	//加载数据
	$scope.load_data 	= function(page){
		if(!page)	page = 1;
		var url 	= config.prefix_url + '/RadioStory/getList?p='+page;
		$http.get(url).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml 	= info.data.page_html;
			$scope.loading 	= false;

			common.startPage($scope.load_data);
		})

		$scope.loading 	= true;
	}

	//添加电台
	$scope.add_item 	= function(item){
		var url 	= config.prefix_url + '/RadioStory/addItem';
		var param 	= item;

		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			$('.modal.add-radio').modal('hide');
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.load_data($scope.nPage);
		})
		$scope.loading 	= true;
	}

	//编辑电台
	$scope.get_item 	= function(radio){
		$scope.nItem 	= radio;
		$('.modal.edit-radio').modal('show');
	}

	//保存编辑
	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url + '/RadioStory/updateItem';
		var param 	= {
			'radio_id'	: id,
			'radio_info'	: info
		}

		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$('.modal.edit-radio').modal('hide');
			$scope.load_data($scope.nPage);
		})

		$scope.loading 	= true;
	}

	// //删除电台
	// $scope.delete 	= function(id){
	// 	var url 	= config.prefix_url + '/RadioStory/updateItem';
	// 	var param 	= {
	// 		'radio_id'	: id,
	// 		'radio_info': {status : -1}
	// 	}
	// 	$http.post(url, param).success(function(info){
	// 		$scope.loading 	= false;
	// 		if(info.code != 1){
	// 			common.dealError(info);
	// 			return false;
	// 		}
	// 		$scope.load_data($scope.nPage);
	// 	})

	// 	$scope.loading 	= true;
	// }

	$scope.load_data();
	$scope.init_js()
})